/**
 * 
 */
package cl.coretech.openbravo.translator.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
import javax.swing.border.Border;

import net.miginfocom.swing.MigLayout;

import org.apache.log4j.Logger;

import cl.coretech.util.ResourceLoader;
import edu.stanford.ejalbert.BrowserLauncher;
import edu.stanford.ejalbert.exception.BrowserLaunchingInitializingException;
import edu.stanford.ejalbert.exception.UnsupportedOperatingSystemException;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
/**
 * @author jsanta
 * 
 */
public class ConversorLibrosAbout extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7464725159345631050L;
	private JButton btnLogo;
	private JButton btnClose;
	private JLabel lblUrl;
	private JScrollPane paneInfo;
	private JTextArea txtInfo;
	private JLabel lblApp;

	private final Logger log = Logger.getLogger(this.getClass());

	/**
	 * @param owner
	 * @param title
	 * @param modal
	 * @throws HeadlessException
	 */
	public ConversorLibrosAbout(Frame owner) throws HeadlessException {
		super(owner, "Acerca de...", true);
		try {
			initComponents();
		} catch (IOException e) {
			log.error("No se pudo incializar la ventana de información");
		}
	}

	private void initComponents() throws IOException {
		MigLayout layout = new MigLayout();
		this.setLayout(layout);
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setModal(true);
		this.setResizable(false);
		this.setTitle("Acerca de...");

		lblApp = new JLabel(AppAboutData.appName);
		lblApp.setFont(new Font("Arial", 1, 16));

		Border border = BorderFactory.createLineBorder(Color.gray, 2);
		btnLogo = new JButton(AppAboutData.getLogoIcon());
		btnLogo.setBorder(border);

		lblUrl = new JLabel(AppAboutData.url);

		txtInfo = new JTextArea(AppAboutData.appInfo, 10, 30);
		txtInfo.setEditable(false);
		txtInfo.setDoubleBuffered(true);
		txtInfo.setLineWrap(true);
		txtInfo.setWrapStyleWord(true);
		txtInfo.setTabSize(4);
		
		paneInfo = new JScrollPane(txtInfo);
		btnClose = new JButton("Cerrar");
		btnClose.setIcon(new ImageIcon(ImageIO.read( ResourceLoader.loadResource("cl/coretech/resources/stop24_h", ".png") )));

		this.add(lblApp, "align center, wrap");
		this.add(btnLogo, "align center, wrap");
		btnLogo.addMouseListener(new MouseAdapter() {
			public void mouseExited(MouseEvent evt) {
				btnLogoMouseExited(evt);
			}
			public void mouseEntered(MouseEvent evt) {
				btnLogoMouseEntered(evt);
			}
		});
		btnLogo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				btnLogoActionPerformed(evt);
			}
		});
		this.add(lblUrl, "align center, wrap");
		lblUrl.addMouseListener(new MouseAdapter() {
			public void mouseExited(MouseEvent evt) {
				lblUrlMouseExited(evt);
			}
			public void mouseEntered(MouseEvent evt) {
				lblUrlMouseEntered(evt);
			}
			public void mouseClicked(MouseEvent evt) {
				lblUrlMouseClicked(evt);
			}
		});
		this.add(paneInfo, "align center, growx, wrap");
		this.add(btnClose, "align center");
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				btnCloseActionPerformed(evt);
			}
		});

		this.pack();

	}

	private void btnLogoActionPerformed(ActionEvent evt) {
		BrowserLauncher launcher;
		try {
			launcher = new BrowserLauncher();
			launcher.openURLinBrowser(lblUrl.getText());
		} catch (BrowserLaunchingInitializingException e) {
			// TODO Auto-generated catch block
			log.error("No se pudo inicilizar el navegador");
		} catch (UnsupportedOperatingSystemException e) {
			// TODO Auto-generated catch block
			log.error("Sistema operativo no soportado");
		}

		// System.out.println("btnLogo.actionPerformed, event="+evt);
		// TODO add your code for btnLogo.actionPerformed
	}

	private void lblUrlMouseClicked(MouseEvent evt) {
		BrowserLauncher launcher;
		try {
			launcher = new BrowserLauncher();
			launcher.openURLinBrowser(lblUrl.getText());
		} catch (BrowserLaunchingInitializingException e) {
			// TODO Auto-generated catch block
			log.error("No se pudo inicilizar el navegador");
		} catch (UnsupportedOperatingSystemException e) {
			// TODO Auto-generated catch block
			log.error("Sistema operativo no soportado");
		}

		// System.out.println("lblUrl.mouseClicked, event="+evt);
		// TODO add your code for lblUrl.mouseClicked
	}

	private void btnCloseActionPerformed(ActionEvent evt) {
		this.setVisible(false);
		this.dispose();

		// System.out.println("btnClose.actionPerformed, event="+evt);
		// TODO add your code for btnClose.actionPerformed
	}
	
	private void btnLogoMouseEntered(MouseEvent evt) {
		Border border = BorderFactory.createLineBorder(Color.black, 2);
		btnLogo.setBorder(border);
	}
	
	private void lblUrlMouseEntered(MouseEvent evt) {
		lblUrl.setText("<html><u>http://www.coretech.cl</u></html>");
	}
	
	private void lblUrlMouseExited(MouseEvent evt) {
		lblUrl.setText("http://www.coretech.cl");
	}
	
	private void btnLogoMouseExited(MouseEvent evt) {
		Border border = BorderFactory.createLineBorder(Color.gray, 2);
		btnLogo.setBorder(border);
	}

}
